/*
 * Copyright (C) 2022 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.cloud.teleport.v2.utils;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import com.google.common.io.Resources;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/** Test cases for the {@link GCSUtils} class. */
public class GCSUtilsTest {

  private static final String AVRO_SCHEMA_FILE_PATH =
      Resources.getResource("SchemaUtilsTest/avro_schema.json").getPath();
  // This file, along with all the necessary protos, is generated by Maven.
  private static final String PROTO_SCHEMA_FILE_PATH =
      Paths.get("target", "generated-test-sources", "protobuf", "schema", "schema.pb").toString();
  @Rule public final TemporaryFolder tmpDir = new TemporaryFolder();

  @Test
  public void testGetFilesInDirectory_withValidPath_returnsPathsOfFilesInDirectory()
      throws Exception {
    File outDir = tmpDir.newFolder("out");
    File outputDir1 = tmpDir.newFolder("out", "unpartitioned_table");
    File outputFile1 =
        new File(outputDir1.getAbsolutePath() + "/" + "output-unpartitioned_table.parquet");
    outputFile1.createNewFile();
    File outputDir2 = tmpDir.newFolder("out", "partitioned_table", "p2_pid=partition");
    File outputFile2 =
        new File(outputDir2.getAbsolutePath() + "/" + "output-partitioned_table-partition.parquet");
    outputFile2.createNewFile();

    List<String> files = GCSUtils.getFilesInDirectory(outDir.getAbsolutePath());
    assertThat(files.size()).isEqualTo(2);
  }

  @Test
  public void testGetGcsFileAsBytes() throws IOException {
    assertArrayEquals(
        getFileBytes(PROTO_SCHEMA_FILE_PATH), GCSUtils.getGcsFileAsBytes(PROTO_SCHEMA_FILE_PATH));
  }

  /**
   * Test whether {@link GCSUtils#getGcsFileAsString(String)} reads a file correctly as a String.
   */
  @Test
  public void testGetGcsFileAsString() {
    String expectedContent =
        "{\n"
            + "  \"type\" : \"record\",\n"
            + "  \"name\" : \"test_file\",\n"
            + "  \"namespace\" : \"com.test\",\n"
            + "  \"fields\" : [\n"
            + "    {\n"
            + "      \"name\": \"id\",\n"
            + "      \"type\": \"string\"\n"
            + "    },\n"
            + "    {\n"
            + "      \"name\": \"price\",\n"
            + "      \"type\": \"double\"\n"
            + "    }\n"
            + "  ]\n"
            + "}\n";
    String actualContent = GCSUtils.getGcsFileAsString(AVRO_SCHEMA_FILE_PATH);

    assertEquals(expectedContent, actualContent);
  }

  /** Convenience method for getting raw bytes from a file. */
  private static byte[] getFileBytes(String filePath) throws IOException {
    return Files.readAllBytes(Paths.get(filePath));
  }
}
